package stringDemo;

import java.util.Scanner;

//将数组里的内容以字符串形式输出
public class StringBuilderTest2 {
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("输入数组长度:");
            int count=sc.nextInt();
            int[]arr= getArray(count);
//          String str=intToString(arr);
//          System.out.println(str);
            String str2=sbToString(arr);
            System.out.println(str2);
    }
    public static String intToString(int[]arr){
        String str="[";
        for (int i = 0; i <arr.length ; i++) {
            str+=arr[i];
            if (i!=arr.length-1){
                str+=",";
            }else {
                str+="]";
            }

        }
        return str;
    }
    public static String sbToString(int []arr){
        StringBuilder sb=new StringBuilder("[");
        for (int i = 0; i <arr.length ; i++) {
            if (i==arr.length-1){
                sb.append(arr[i]+"]");
            }else {
                sb.append(arr[i]+",");
            }
        }
        String str=sb.toString();
        return str;

    }
    public static int[] getArray(int count){
        Scanner scanner=new Scanner(System.in);
        int[]arr=new int[count];
        for (int i = 0; i <arr.length ; i++) {
            int num=scanner.nextInt();
            arr[i]=num;
        }
        return arr;
    }
}
